package com.gton.lpl.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gton.lpl.entity.TTransferManage;
import com.gton.lpl.entity.dto.PageRespDto;
import com.gton.lpl.entity.dto.TTransferManageDTO;
import com.gton.lpl.mapper.TTransferManageMapper;
import com.gton.lpl.service.TTransferManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 副表 服务实现类
 * </p>
 *
 * @author GuoTong
 * @since 2022-08-04
 */
@Service
@DS("slave_1")
public class TTransferManageServiceImpl extends ServiceImpl<TTransferManageMapper, TTransferManage> implements TTransferManageService {

    @Resource
    private TTransferManageMapper tTransferManageMapper;

    @Override
    public PageRespDto selectByWhereParam(TTransferManageDTO tTransferManageDTO) {
        PageRespDto pageRespDto = new PageRespDto();
        String currentCode = tTransferManageDTO.getCurrentCode();
        // 获取当前页
        int pageNum = tTransferManageDTO.getPageNum();
        // 计算limit 第一个参数  公式（(pageNum - 1) * pageSize）
        tTransferManageDTO.setPageNum((pageNum - 1) * tTransferManageDTO.getPageSize());
        // 默认给与查询当前用户的第一阶段
        if (StringUtils.isEmpty(currentCode)) {
            // 获取总条数
            long totalNum = tTransferManageMapper.selectListByGrupStatCount(tTransferManageDTO);
            pageRespDto.setTotal(totalNum);
            List<TTransferManage> records =  tTransferManageMapper.selectListByGrupState(tTransferManageDTO);
            pageRespDto.setRecords(records);
        }else{
            //携带有状态未隔离的情况:隔离自己状态
            if (StringUtils.equals(currentCode,StateEnum.RestoreInitiated.value)){
                tTransferManageDTO.setInitiator(tTransferManageDTO.getUserName());
            } // 否则是其他状态就是全量
            long totalNum = tTransferManageMapper.selectListByStateEnumCount(tTransferManageDTO);
            pageRespDto.setTotal(totalNum);
            List<TTransferManage> records =  tTransferManageMapper.selectListByStateEnum(tTransferManageDTO);
            pageRespDto.setRecords(records);
        }
        pageRespDto.setPageNum(tTransferManageDTO.getPageNum());
        pageRespDto.setPageSize(tTransferManageDTO.getPageSize());
        return pageRespDto;

    }


    /**
     * Description: 状态枚举
     *
     * @author: GuoTong
     * @date: 2022-08-04 19:17:28
     */
    enum StateEnum {
        //1代表待还原发起
        RestoreInitiated("RestoreInitiated", "1"),
        //2代表硅芯制备处理
        SiliconCorePreparation("SiliconCorePreparation", "2"),
        //3代表还原处理
        RestoreProcessing("RestoreProcessing", "3"),
        //4代表成品处理'
        FinishedProductHandling("FinishedProductHandling", "4"),
        ;
        private String msg;
        private String value;

        StateEnum(String msg, String value) {
            this.msg = msg;
            this.value = value;
        }
    }
}
